<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:10
 */

namespace App\Ordersys\Controllers\Admin;

use App\Base\Controllers\Controller;

use App\Ordersys\Contracts\OrdersysCircleFriendsInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class OrderCircleFriendsController extends Controller
{

    /**
     * 朋友圈接口
     * @return OrdersysCircleFriendsInterface
     */
    public function getOrdersysCircleFriendsInterface()
    {
        return app()->make(OrdersysCircleFriendsInterface::class);
    }

    /**
     * 朋友圈列表
     */
    public function index(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $title = $request->input("title");
                if (!empty($title)) {
                    $query->where("title", "like", '%' . $title . '%');
                }
                $type = $request->input("type");
                if (!empty($type)) {
                    $query->where("type", $type);
                }
            };

            return $this->getOrdersysCircleFriendsInterface()->getList($where);
        }
        return view("ordersys.circle_friends.index");
    }

    /**
     * 朋友圈添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $validator = Validator::make($request->all(), [
                'title' => 'required',
                'show_val' => 'required',
            ], ["title.required" => '标题必须有值', "show_val.required" => '附件是必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return $this->getOrdersysCircleFriendsInterface()->saveData($request->all());
        }
        return view("ordersys.circle_friends.add");
    }

    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \App\Base\Exceptions\ApiException
     *
     */
    public function edit(Request $request)
    {
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'title' => 'required',
                'show_val' => 'required',
            ],["title.required"=>'标题必须有值',"show_val.required"=>'标题必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getOrdersysCircleFriendsInterface()->saveData($request->all());
        }
        $data=$this->getOrdersysCircleFriendsInterface()->getInfo($id);
        return view("ordersys.circle_friends.edit")->with("data",$data);
    }

    /**
     * 广告删除
     * @param Request $request
     */
    public function del(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where=function($query) use ($request){
                $id=$request->input("id");
                $query->where("id",$id);
                if(UID!=1){
                    $query->where('uid',UID);
                }
            };
            if($rdata=$this->getOrdersysCircleFriendsInterface()->deleteData($where)){
                return $rdata;
            }else{
              throw  new \Exception("此条记录删除可能不是您发布无法删除");
            }
        }
    }

    /**
     * 新增音频文件
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Base\Exceptions\ApiException
     */
    public function add_vedio(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $validator = Validator::make($request->all(), [
                'title' => 'required',
                'show_val' => 'required',
            ], ["title.required" => '标题必须有值', "show_val.required" => '附件是必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return $this->getOrdersysCircleFriendsInterface()->saveData($request->all());
        }
        return view("ordersys.circle_friends.addvedio");
    }

    /**
     * 接受音频文件
     * @param Request $request
     */
    public function accept(Request $request)
    {
        $data = $request->all();
        $data["uid"] = UID;
        return $this->getOrdersysCircleFriendsInterface()->acceptData($data);
    }

    /**
     * 展示内容
     * @param Request $request
     */
    public function show(Request $request){

        $id=$request->input("id");

        $data=$this->getOrdersysCircleFriendsInterface()->getInfo($id);

        return view("ordersys.circle_friends.show")->with("data",$data);
    }


}